import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { Http } from '@angular/http';
import { InitService } from '../../../public/serve/init/init.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { DatePipe } from '@angular/common';
import { Service } from 'app/server/server';
import { FileUploader } from 'ng2-file-upload';

declare var $: any;
@Component({
    selector: 'app-keyword-table-import',
    templateUrl: './keyword-table-import.component.html'
})

export class KeywordTableImportComponent implements OnInit {
    @Output() inportOutput = new EventEmitter<void>();
    uploader: FileUploader;
    hasBaseDropZoneOver: boolean;
    hasAnotherDropZoneOver: boolean;
    response: string;
    loading: boolean;

    constructor(
        private server: Service,
        public initService: InitService,
        public removeTableService: RemoveTableService,
        private hintsService: HintsService,
        private request: CudTableService,
        private datePipe: DatePipe,
        private http: Http,
    ) {
        //https://github.com/valor-software/ng2-file-upload/blob/development/demo/src/app/components/file-upload/simple-demo.ts
        this.uploader = new FileUploader({
            url: this.server.baseUrl + "Service/ThemeWords/ImportThemeTable",
            method: "POST",
            itemAlias: "importfile",
        });

        this.uploader.response.subscribe(res => this.response = res);
    }

    ngOnInit() {

    }

    public fileOverBase(e: any): void {
        this.hasBaseDropZoneOver = e;
    }

    public fileOverAnother(e: any): void {
        this.hasAnotherDropZoneOver = e;
    }

    ImportUpload() {
        if(this.uploader.queue[0]){
            if(this.uploader.queue[0].file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || this.uploader.queue[0].file.type == 'application/vnd.ms-excel'){
                // 上传
                this.loading = true;
                let obj = this;
                this.uploader.queue[0].onSuccess = function (response, status, headers) {
                    // 上传文件成功
                    if (status == 200) {
                        // 上传文件后获取服务器返回的数据
                        let tempRes = JSON.parse(response);
                        obj.hintsService.messages(tempRes.message, tempRes.result_code);
                        obj.inportOutput.emit(tempRes.result_code);
                        obj._init();
                        obj.uploader.queue[0].remove();
                    } else {
                        // 上传文件后获取服务器返回的数据错误
                        obj.hintsService.messages('上传文件失败', '9999');
                    }
                    obj.loading = false;
                };
                this.uploader.queue[0].onError = function (response, status, headers) {
                    obj.hintsService.messages('上传文件失败', '9999');
                    obj.loading = false;
                };
                this.uploader.queue[0].upload(); // 开始上传
            }else{
                this.hintsService.messages('请选择‘xls’或‘xlsx’格式的导入文件。', '9999');
            }
        }else{
            this.hintsService.messages('文件不能为空！请选择‘xls’或‘xlsx’格式的导入文件。', '9999');
        }
    }
    /**
      * 初始化
      */
    private _init() {
        this.initService._init();
        $("#keywordTableImport").modal("hide");
    }



}
